package com.lizonghui.googleplay.fragment;

import android.os.SystemClock;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.lizonghui.googleplay.adapter.ItemAdapter;
import com.lizonghui.googleplay.base.BaseFragment;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.bean.DownLoadInfo;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.control.ItemHolder;
import com.lizonghui.googleplay.factory.ListViewFactory;
import com.lizonghui.googleplay.manager.DownLoadManger;
import com.lizonghui.googleplay.protocol.AppProtocol;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/6/16.
 */
public class AppFragment extends BaseFragment {


    private AppProtocol mAppProtocol;
    private List<ItemInfoBean> mItemInfoBeans;
    private AppAdapter mAdapter;

    @Override
    public View initSuccessView() {
        ListView listView = ListViewFactory.createListView();
        mAdapter = new AppAdapter(listView, mItemInfoBeans);
        listView.setAdapter(mAdapter);
        return listView;
    }


    //baseAdapter-->MyBaseAdapter-->SuperBaseAdapter(封装了getView，ViewHolder,上滑加载更多)
    private class AppAdapter extends ItemAdapter {
        public AppAdapter(AbsListView absListView, List datas) {
            super(absListView, datas);
        }

        //上滑加载更多数据，分页请求数据
        @Override
        public List initLoadMoreData() throws Exception {
            List<ItemInfoBean> itemInfoBeans = mAppProtocol.loadData(mItemInfoBeans.size());
            if(itemInfoBeans != null){
                return itemInfoBeans;
            }
            return null;
        }
    }
    @Override
    public LoadingPager.LoadedResultState initData() {
        SystemClock.sleep(2000);// 模拟网络耗时2s种
        //请求数据，想到protocol请求
        mAppProtocol = new AppProtocol();
        try {
            //加载数据
            mItemInfoBeans = mAppProtocol.loadData(0);
            //校验数据
            LoadingPager.LoadedResultState resultState = checkResData(mItemInfoBeans);
            return resultState;

        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        //移除所有观察者,观察者在holder中建立，并通过adapter返回给ListView.所以用adapter作为判断位
        if(mAdapter != null){
            List<ItemHolder> itemHolders = mAdapter.itemHolders;
            for (ItemHolder itemHolder:itemHolders) {
                DownLoadManger.getInstance().deleteObserver(itemHolder);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //添加所有观察者
        if(mAdapter != null){
            List<ItemHolder> itemHolders = mAdapter.itemHolders;
            for (ItemHolder itemHolder:itemHolders) {
                DownLoadManger.getInstance().addObserver(itemHolder);


                //根据itemHolder的数据，得到downLoadInfo
                DownLoadInfo downLoadInfo = DownLoadManger.getInstance().getDownLoadInfo(itemHolder.mInfoBean);
                //手动发布最新消息，让UI刷新
                DownLoadManger.getInstance().notifyObservers(downLoadInfo);//要拿到downLoadInfo,而我们有
            }
        }
    }

}

